<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username"></el-input>
      <el-input style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-message" class="ml-5"
                v-model="email"></el-input>
      <el-input style="width: 200px" placeholder="请输入昵称" suffix-icon="el-icon-position" class="ml-5"
                v-model="nickname"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
      <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>
      <el-button type="primary">导出 <i class="el-icon-top"></i></el-button>
    </div>

    <el-table
        :data="tableData"
        border
        stripe
        :highlight-current-row="true"
        @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"></el-table-column>

      <el-table-column prop="username" label="账号">
      </el-table-column>
      <el-table-column prop="nickname" label="昵称">
      </el-table-column>
      <el-table-column prop="email" label="邮箱">
      </el-table-column>

      <el-table-column prop="mobile" label="电话">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-button type="info" @click="asignRole(scope.row.id,scope.row.username)">分配角色</el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>
    <div class="block" style="padding-top: 10px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--    增加，修改表单-->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.mobile" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <!--分配角色-->
    <el-dialog
        title="分配角色"
        :visible.sync="dialogVisible"
        width="50%"
    >
      <span class="quote">
       人员： {{ this.assignUserName }}

        <el-row style="padding-top: 50px">
<!--          已分配角色-->
  <el-col :span="12" style="padding-right:2px">
    已分配角色
        <el-table
            :data="assignedRoles"
            style="width: 100%"
            :header-cell-style="{background:'#E4E4E4',borderColor:'#CECECE'}">

      <el-table-column
          prop="targetName"
          label="角色名"
      >
      </el-table-column>
      <el-table-column
          label="操作">
                <template slot-scope="scope">

        <el-button type="text" style="color:#ff5722;font-size:15px" @click="unAssignRole(scope.row.id)">移除</el-button>
                </template></el-table-column>
    </el-table>
  </el-col>
          <!--          未分配角色-->
  <el-col :span="12">
    未分配角色
            <el-table
                :data="unAssignedRoles"
                style="width: 100%"
                :header-cell-style="{background:'#E4E4E4',borderColor:'#CECECE'}">

      <el-table-column
          prop="name"
          label="角色名"
      >
      </el-table-column>
      <el-table-column
          label="操作">
                <template slot-scope="scope">

                <el-button type="text" style="color:#409EFF;font-size:15px"
                           @click="assignRole(scope.row.id,scope.row.name)">分配</el-button>
                </template>
      </el-table-column>
    </el-table>
  </el-col>
</el-row>
      </span>

      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from '@/request/index.js'

export default {
  name: "UserList",
  data() {

    return {
      tableData: [],
      pageSize: 5,
      pageNum: 1,
      total: 0,
      username: "",
      email: "",
      nickname: "",
      dialogFormVisible: false,
      form: {},
      multipleSelection: [],
      dialogVisible: false,
      assignedRoles: [],
      unAssignedRoles: [],
      assignUserId: "",
      assignUserName: ""


    }
  },
  methods: {
    // 分配角色按钮点击事件
    asignRole(userId, userName) {
      this.dialogVisible = true;
      this.assignList(userId);
      this.assignUserId = userId;
      this.assignUserName = userName;

    },
    assignRole(roleId, roleName) {
      axios.post('role/assignRole', {
        userId: this.assignUserId,
        userName: this.assignUserName,
        roleId: roleId,
        roleName: roleName
      }).then((res) => {
        console.log(res)
        this.assignList(this.assignUserId);
      })
    },
    unAssignRole(id) {
      axios.post('role/removeRole', {
        id: id
      }).then((res) => {
        console.log(res)
        this.assignList(this.assignUserId);
      })
    },
    /**
     * 分配角色页面list
     * @param id
     */
    assignList(userId) {
      //未分配角色
      axios.post('role/unBindRoleList', {
        userId: userId
      }).then((res) => {
        console.log(res)
        this.unAssignedRoles = res.data;
      })

      //已分配角色
      axios.post('role/bindRoleList', {
        userId: userId
      }).then((res) => {
        console.log(res)
        this.assignedRoles = res.data;
      })

    },
    /**
     * 刷新列表
     */
    load() {
      axios.post('user/getUsersByPage', {
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        username: this.username,
        nickname: this.nickname,
        email: this.email

      }).then((res) => {
        console.log(res)
        this.tableData = res.data.records;
        this.total = res.data.total;
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      axios.post("/user/del/batch", ids).then(res => {
        if (res.code === 200) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    /**
     * 每页数据数量改变事件
     * @param val
     */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load();
    },
    /**
     * 页数改变事件
     * @param val
     */
    handleCurrentChange(val) {
      this.pageNum = val;
      this.load();
      console.log(`当前页: ${val}`);
    },
    reset() {
      this.nickname = "";
      this.email = "";
      this.username = "";
      this.load();
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    save() {
      axios.post("/user/saveUser", this.form).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    },
    del(id) {
      axios.post("/user/deleteById", {
        id: id
      }).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
  },
  created() {
    this.load();
  }
}
</script>

<style scoped>
.quote {
  background-color: #f2f2f2;
  border-left: 5px solid #1890ff;
  height: 40px;
  line-height: 40px;
  width: 100%;
  font-size: 20px;
  display: block;
}
</style>
